Ccay.Module.define("ccay.security.permission.tree",function(page,$S){
	var popBean, zTreeObject;
	
	page.init = function(bean){
		popBean = bean;
		//zTree setting
		var setting = {
			check: {
				enable: true,
				chkStyle: popBean.isMulti ? "checkbox" : "radio",
				chkboxType: {"Y": "ps", "N": "ps"},
				radioType : "all"
			},
			data: {
				key: {
					name : "name",
					children : "children",
					title : "title",
					checked : "checked"
				},
				simpleData: {
					enable: false
				}
			}
		};
		
		Ccay.doGet("services/manage/security/permissionManage/findPermissionTree", '', function (resources) {
			//单选时只能选择叶子
			if(!popBean.isMulti){
				setting.callback = {
					beforeCheck : function(treeId,treeNode){
						if(!treeNode.permissionString && treeNode.isParent){
							zTreeObject.expandNode(treeNode);
						}
						return !!treeNode.permissionString;
					}
				};
			}
			//初始化树
			zTreeObject = Ccay.tree.init($("#permissionSelectTree"), setting,resources,false,null);
			//根据显示的值选中节点
			var perStr = popBean.selectedItems, permissionList, permissionStr;
			if (typeof perStr == "string"){
				permissionList = popBean.isMulti ? perStr.split(popBean.splitChar) : [perStr];
			}else{
				permissionList = perStr;
			}
			if (permissionList && permissionList.length){
				for(var i=0; i<permissionList.length; i++) {
					tempNode = zTreeObject.getNodesByParam("permissionString", permissionList[i]);
					zTreeObject.checkNode(tempNode, true, true);
				}
			}
		});
	};
	
		
	
	//确定按钮单击事件
	page.onOk = function () {
		var noSelect = true;
		var treeNodes = Ccay.tree.getSubCheckedNodes(zTreeObject, true);
		if (treeNodes.length)
		{
			noSelect = false;
			var permissionArray = new Array();
			var treeNode,permissionObj;
			for(var i=0; i<treeNodes.length; i++){
				treeNode = treeNodes[i];
				permissionArray.push({value:treeNode.permissionString,text:treeNode.permissionName});
			}
			popBean.callback && popBean.callback(permissionArray);
			page.closeDialog && page.closeDialog();
		}
		else
		{
			noSelect = true;
		}
	
		if (noSelect)
		{
			Ccay.UI.topBox("ccay.security.message.noSelectedNode");
			return ;
		}
	};
});